/**
 * Copyright 2011 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.google.solarchallenge.server.domain;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.solarchallenge.shared.dtos.UsAddressDto;
import com.google.solarchallenge.shared.exceptions.InvalidClientInputException;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

/**
 * Domain object for UsAddress.
 *
 * @author Arjun Satyapal
 */
@PersistenceCapable(detachable = "true")
public class UsAddressDao implements AbstractDao {
  @PrimaryKey
  @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
  private Key key;
  @Persistent
  private String address1;
  @Persistent
  private String address2;
  @Persistent
  private String city;
  @Persistent
  private String state;
  @Persistent
  private String zip;

  @Override
  public StringBuilder getStringBuilder() {
    StringBuilder builder = new StringBuilder("UsAddressDao[")
        .append("key:").append(KeyFactory.keyToString(key))
        .append(", address1:").append(address1)
        .append(", address2:").append(address2)
        .append(", city:").append(city)
        .append(", state:").append(state)
        .append(", zip:").append(zip)
        .append("].");

    return builder;
  }

  @Override
  public String toString() {
    return getStringBuilder().toString();
  }

  private UsAddressDao() {
  }

  @Override
  public String validate() {
    StringBuilder builder = new StringBuilder("");

    // id will be set only by server. So not checking it.
    if (address1.isEmpty()) {
      builder.append("address1 is empty.\n");
    }
    if (address2.isEmpty()) {
      builder.append("address2 is empty.\n");
    }
    if (city.isEmpty()) {
      builder.append("city is empty.\n");
    }
    if (state.isEmpty()) {
      builder.append("state is empty.\n");
    }
    if (zip.isEmpty()) {
      builder.append("zip is empty.\n");
    }

    return builder.toString();
  }

  public String getAddress1() {
    return address1;
  }

  public String getAddress2() {
    return address2;
  }

  public String getCity() {
    return city;
  }

  public String getState() {
    return state;
  }

  public String getZip() {
    return zip;
  }


  public static UsAddressDao fromDto(UsAddressDto dto, boolean isCreate) {
    if (!dto.validate().isEmpty()) {
      throw new InvalidClientInputException(
          "Invalid UsAddressDao with Error : [" + dto.validate() + "].");
    }
    if (isCreate && dto.getId() != 0) {
      throw new InvalidClientInputException("Invalid Id[" + dto.getId()
        + "]. Only server is allowed to set the id. "
        + "Clients should not set it.");
    }

    UsAddressDao usAddressDao = new UsAddressDao();
    usAddressDao.address1 = dto.getAddress1();
    usAddressDao.address2 = dto.getAddress2();
    usAddressDao.city = dto.getCity();
    usAddressDao.state = dto.getState();
    usAddressDao.zip = dto.getZip();

    return usAddressDao;
  }

  public UsAddressDto toDto() {
    return new UsAddressDto.Builder()
        .setAddress1(address1)
        .setAddress2(address2)
        .setCity(city)
        .setState(state)
        .setZip(zip)
        .build();
  }
}
